//
//  ZCTBC.m
//  BaiSi
//
//  Created by 张灿 on 16/4/24.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCTBC.h"
#import "ZCEssenceViewController.h"
#import "ZCNewViewController.h"
#import "ZCPushViewController.h"
#import "ZCAttentionViewController.h"
#import "ZCMeTableViewController.h"
#import "ZCTabBar.h"

#import "ZCNavigationController.h"

@interface ZCTBC ()<UITabBarControllerDelegate>

@property (nonatomic, strong) UIViewController *lastSelectedVC;

@end

@implementation ZCTBC


- (void)viewDidLoad {
    [super viewDidLoad];
    //添加所有子控制器
    [self setupAllChildVC];
    //设置所有的TabBarItem
    [self setupAllTabBarItem];
    //设置自定义TabBar
    [self setupTabBar];
    
    self.delegate = self;
 
}
#pragma mark - 监听tabBar按钮重复点击
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (_lastSelectedVC == viewController) {
        //如果是重复点击就发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:ZCRepeatClickTabTitleNote object:nil];
    }
    
    _lastSelectedVC = viewController;
}

//设置tabbar字体
+ (void)load
{
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
    //颜色
    dict1[NSForegroundColorAttributeName] = [UIColor colorWithRed:81 / 255.0 green:81 / 255.0 blue:81 / 255.0 alpha:1.0];
    //选中
    [[UITabBarItem appearance] setTitleTextAttributes:dict1 forState:UIControlStateSelected];
    
    
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
    //字体,注意只能设置常态下的字体大小,并且对选中字体同样适用
    dict2[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    //未选中
    [[UITabBarItem appearance] setTitleTextAttributes:dict2 forState:UIControlStateNormal];
    
}

//设置所有的TabBarItem
- (void)setupAllTabBarItem
{
    //精华
    UINavigationController *nav0 = self.childViewControllers[0];
    nav0.tabBarItem.title = @"精华";
    nav0.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
    nav0.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabBar_essence_click_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //最新
    UINavigationController *nav1 = self.childViewControllers[1];
    nav1.tabBarItem.title = @"新帖";
    nav1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
    nav1.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabBar_new_click_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    

    //关注
    UINavigationController *nav3 = self.childViewControllers[2];
    nav3.tabBarItem.title = @"关注";
    nav3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
    nav3.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabBar_friendTrends_click_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //我
    UINavigationController *nav4 = self.childViewControllers[3];
    nav4.tabBarItem.title = @"我";
    nav4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
    nav4.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabBar_me_click_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

//添加所有子控制器
- (void)setupAllChildVC
{
    //精华
    ZCEssenceViewController *essenceVC = [[ZCEssenceViewController alloc] init];
    [self addChildViewController:essenceVC];
    
    
    //最新
    ZCNewViewController *newVC = [[ZCNewViewController alloc] init];

    [self addChildViewController:newVC];
    
    
    //关注
    ZCAttentionViewController *attentionVC = [[ZCAttentionViewController alloc] init];

    [self addChildViewController:attentionVC];
    
    //我
//    ZCMeTableViewController *meVC = [[ZCMeTableViewController alloc] init];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"ZCMeTableViewController" bundle:nil];
    ZCMeTableViewController *meVC = [storyBoard instantiateInitialViewController];

    [self addChildViewController:meVC];
}

//重写添加子控制器方法,在里面统一添加 自定义的导航控制器
- (void)addChildViewController:(UIViewController *)childController
{
    ZCNavigationController *nav = [[ZCNavigationController alloc] initWithRootViewController:childController];
    [super addChildViewController:nav];
}

- (void)setupTabBar
{
    //创建自定义的TabBar
    ZCTabBar *tabbar = [[ZCTabBar alloc] init];
    //利用KVC替换系统自带的tabbar
    [self setValue:tabbar forKey:@"tabBar"];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
